package com.edu.control.deploy.retrytask.impl;

import com.edu.control.deploy.CommonMethod;
import com.edu.control.deploy.DeployStateSync;
import com.edu.control.deploy.DeployStorage;
import com.edu.control.deploy.PodInfo;
import com.edu.control.deploy.PodResult;
import com.edu.control.deploy.constant.DeployStatus;
import com.edu.control.deploy.retrytask.RetryException;
import com.edu.control.deploy.retrytask.Task;
import com.edu.control.k8s.KubernetesApi;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class K8SFuncTask extends CommonMethod implements Task {

    public static final Logger LOGGER = LoggerFactory.getLogger(K8SFuncTask.class);

    private PodInfo podInfo;

    private KubernetesApi kubernetesApi;

    private DeployStorage deployStorage;

    private DeployStateSync deployStateSync;

    public K8SFuncTask(PodInfo podInfo, KubernetesApi kubernetesApi, DeployStorage deployStorage, DeployStateSync deployStateSync) {
        this.podInfo = podInfo;
        this.kubernetesApi = kubernetesApi;
        this.deployStorage = deployStorage;
        this.deployStateSync = deployStateSync;
    }

    @Override
    public void fire() throws Exception {

        this.podInfo.times++;
        V1PodList v1PodList = getV1PodList(kubernetesApi);
        if (v1PodList != null) {
            PodResult podResult = new PodResult();
            podResult.expectNum = this.podInfo.getExpectNum();
            List<V1Pod> items = v1PodList.getItems();
            if (items != null && !items.isEmpty()) {
                podResult.actualNum = items.size();
                loopV1Pods(items, podResult);
            }
            this.podInfo.setLastPodResult(podResult);
            if (podResult.runningNum == this.podInfo.getExpectNum()) {
                store(deployStorage, podInfo, DeployStatus.SUCCESS, podResult, LOGGER);
                deployStateSync.sync(this.podInfo);
            } else {
                store(deployStorage, podInfo, DeployStatus.PART_POD, podResult, LOGGER);
                throw new RetryException(podResult.toString());
            }
        } else {
            store(deployStorage, podInfo, DeployStatus.PART_POD, null, LOGGER);
            throw new RetryException("V1PodList is empty");
        }
    }


    @Override
    public void onceRetry() throws Exception {

    }

    @Override
    public void exceedTimesRetry() throws Exception {
        store(deployStorage, podInfo, DeployStatus.LIMITED_TIME, this.podInfo.getLastPodResult(), LOGGER);
    }

    @Override
    public void interrupt() throws Exception {
        store(deployStorage, podInfo, DeployStatus.INTERRUPT, null, LOGGER);
    }

    @Override
    public String getNamespace() {
        return this.podInfo.getNamespace();
    }

    @Override
    public String getLabels() {
        return this.podInfo.getLabels();
    }

    @Override
    public String toString() {
        return this.podInfo.getFunctionName();
    }
}
